package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.qingcheng.pojo.goods.Goods;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.service.goods.SpuService;
import com.qingcheng.service.index.IndexService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Reference
    private SpuService spuService;


    //商品上架,增加商品索引数据到elasticsearch
    public void addIndex(String spuId) {
        //查询要新增的sku对象
        List<Sku> skuList = getSkuList(spuId);
        //封装查询请求,商品id不存在时,新增
        BulkRequest bulkRequest = new BulkRequest();
        for (Sku sku : skuList) {
            IndexRequest indexRequest = new IndexRequest("sku", "doc", sku.getId());
            Map skuMap = new HashMap();
            skuMap.put("name", sku.getName());
            skuMap.put("brandName", sku.getBrandName());
            skuMap.put("image", sku.getImage());
            skuMap.put("spuId", sku.getSpuId());
            skuMap.put("categoryName", sku.getCategoryName());
            skuMap.put("price", sku.getPrice());
            skuMap.put("createTime", sku.getCreateTime());
            skuMap.put("saleNum", sku.getSaleNum());
            skuMap.put("commentNum", sku.getCommentNum());
            Map spec = JSON.parseObject(sku.getSpec());
            skuMap.put("spec", spec);
            indexRequest.source(skuMap);
            bulkRequest.add(indexRequest);
        }
        try {
            BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
            int status = bulkResponse.status().getStatus();
            System.out.println(status);
//            restHighLevelClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //商品下架,从elasticsearch中删除商品索引数据
    public void deleteIndex(String spuId) {
        List<Sku> skuList = getSkuList(spuId);
        BulkRequest bulkRequest = new BulkRequest();

        for (Sku sku : skuList) {
            DeleteRequest deleteRequest = new DeleteRequest("sku", "doc", sku.getId());
            bulkRequest.add(deleteRequest);
        }
        try {
            BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
            int status = bulkResponse.status().getStatus();
            System.out.println(status);
//            restHighLevelClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public List<Sku> getSkuList(String spuId) {
        Goods goods = spuService.findGoodsById(spuId);
        return goods.getSkuList();
    }
}
